//
//  STCycleAnimator.m
//  STBest
//
//  Created by apple on 2017/8/29.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "STCycleAnimator.h"
@interface STCycleAnimator()<CAAnimationDelegate>


@end
@implementation STCycleAnimator{
    __weak id<UIViewControllerContextTransitioning> _transitionContext;
}
#pragma mark - UIViewControllerAnimatedTransitioning -
// 提供专场动画时长
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    
    return 0.5;
}

// 编写具体的专场动画
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
   _transitionContext = transitionContext;
    // 1. 获取动画容器
    UIView *containerView = [transitionContext containerView];
    
    // 2. 获取源视图和目标视图
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    // 还能拿到视图控制器
    UIViewController *fromeVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    // 获取将要打开的视图
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    // 还能拿到将要的视图控制器
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // 如果需要2个视图进行动画就必须都在视图容器内
    // 难点：fromeView 和 toView 上下层次关系
    if(self.isPresented){
        [containerView addSubview:toView];
    }else{
        [containerView insertSubview:toView atIndex:0];
    }
    
    
    // 专场方式
    // 思路让2个视图叠加，同时有一个圆漏空显示
    // 可以采用一种遮罩的形式
    // 思路:
    // 1. 你设置一个视图作为一个遮罩视图 -> 只要遮罩到的地方就可以显示出来
    // 2. 想办法改变遮罩的半径
    // 3. 计算结束半径长度
    
    // 创建遮罩层 -> 遮罩中部分才显示
    CAShapeLayer *masKLayer = [CAShapeLayer new];
    
    //中心点
    CGPoint centerPoint = self.centerPoint;
    
    
    
    //计算宽度
    CGFloat width = MAX(centerPoint.x, [UIScreen mainScreen].bounds.size.width - centerPoint.x);
    
    // 计算高度
    CGFloat height = MAX(centerPoint.y, [UIScreen mainScreen].bounds.size.height - centerPoint.y);
    
    // 通过勾股定理计算
    CGFloat maxRadius = sqrt(pow(width, 2) + pow(height, 2));
    
    //半径
    CGFloat beginRadius = self.isPresented ? 0 : maxRadius;
    
    // 通过勾股定理计算
    CGFloat endRadius = self.isPresented ? maxRadius : 0;
    
    // 创建圆形绘制路径 // clockwise 是否顺时针
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:beginRadius startAngle:0 endAngle:360 clockwise:YES];
    
    UIBezierPath *endPath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:endRadius startAngle:0 endAngle:360 clockwise:YES];
    
    //创建一个keypath动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];

    animation.fromValue = (__bridge id _Nullable)([beginPath CGPath]);
    animation.toValue = (__bridge id _Nullable)([endPath CGPath]);
    
    //设置动画时长
    animation.duration = [self transitionDuration:transitionContext];
    
    //设置动画向前填充
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    
    //设置代理完成后完成动画
    animation.delegate = self;
    
    //设置层的遮罩
    //遮罩应用目标色弧度
    if(self.isPresented){
        toView.layer.mask = masKLayer;
    }else{
        fromView.layer.mask = masKLayer;
    }
    
    [masKLayer addAnimation:animation forKey:@"aim"];
}
#pragma mark - CAAnimationDelegate -
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    // 一旦使用自定义专场动画，苹果API会事件冻结，所以当我们完成动画以后需要解除冻结
    [_transitionContext completeTransition:YES];
    
}
@end
